package cn.iocoder.yudao.module.system.controller.admin.areaserve;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.areaserve.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.areaserve.*;
import cn.iocoder.yudao.module.system.service.areaserve.AreaReserveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "按区域预定工位")
@RestController
@RequestMapping("/system/area-reserve")
@Validated
@Slf4j
public class AreaReserveController {

    @Resource
    private AreaReserveService areaReserveService;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @GetMapping("/get")
    @Operation(summary = "连接测试")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<String> getHello(@RequestParam("id") Long id) {
        return success("Hello World" + String.valueOf(id) );
    }

    @GetMapping("/getAreaDepartementList")
    @Operation(summary = "获得办公区域与部门对照关系")
    @Parameter(name = "officeName", description = "办公室名称", required = true)
    public CommonResult<List<AreaDepartementDO>> getAreaDepartementList(@Valid AreaDepartementPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AreaDepartementDO> pageResult = areaReserveService.getAreaDepartementList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, AreaDepartementDO.class));

    }

    @GetMapping("/getAreaResourcesList")
    @Operation(summary = "获得办公坐席开放资源")
    @Parameter(name = "officeName", description = "办公室名称", required = true)
    public CommonResult<List<AreaResourcesDO>> getAreaResourcesList(@Valid AreaResourcesPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AreaResourcesDO> pageResult = areaReserveService.getAreaResourcesList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, AreaResourcesDO.class));

    }

    @GetMapping("/getAreaWorkdayList")
    @Operation(summary = "获得办公坐席开放资源")
    @Parameter(name = "officeName", description = "办公室名称", required = true)
    public CommonResult<List<AreaWorkdayDO>> getAreaWorkdayList(@Valid AreaWorkdayPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AreaWorkdayDO> pageResult = areaReserveService.getAreaWorkdayList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, AreaWorkdayDO.class));

    }

    @GetMapping("/getAreaBookingQuotaList")
    @Operation(summary = "获得具体某一日可预订的坐席")
    @Parameter(name = "officeName", description = "办公室名称", required = true)
    @Parameter(name = "areaName", description = "区域名称", required = true, example = "A")
    @Parameter(name = "recentDays", description = "获取最近几天的数据", required = true, example = "3")
    public CommonResult<List<AreaBookingQuotaDO>> getAreaBookingQuotaList(@Valid AreaBookingQuotaPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
        } catch (ParseException e) {
            pageReqVO.setResourceDate(new Date());
        }
        List<AreaBookingQuotaDO> pageResult = areaReserveService.getAreaBookingQuotaList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, AreaBookingQuotaDO.class));

    }

    @GetMapping("/getBookingSeatRecordList")
    @Operation(summary = "获得具体某一日已预订坐席数量")
    @Parameter(name = "officeName", description = "办公室名称", required = true)
    public CommonResult<List<BookingSeatRecordDO>> getBookingSeatRecordList(@Valid BookingSeatRecordPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookingSeatRecordDO> pageResult = areaReserveService.getBookingSeatRecordList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, BookingSeatRecordDO.class));

    }

    @GetMapping("/getMdmEmployeeList")
    @Operation(summary = "获得用户主数据")
    @Parameter(name = "employeeCode", description = "员工编码", required = true)
    public CommonResult<List<MdmEmployeeDO>> getMdmEmployeeList(@Valid MdmEmployeePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdmEmployeeDO> pageResult = areaReserveService.getMdmEmployeeList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, MdmEmployeeDO.class));

    }

    @GetMapping("/syncEmployeeData")
    @Operation(summary = "获得用户主数据")
    @Parameter(name = "employeeCode", description = "员工编码", required = true)
    public CommonResult<List<MdmEmployeeDO>> syncEmployeeData(@Valid MdmEmployeePageReqVO pageReqVO) {
        areaReserveService.syncEmployeeData();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdmEmployeeDO> pageResult = areaReserveService.getMdmEmployeeList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, MdmEmployeeDO.class));

    }

    @GetMapping("/getIsHolidayList")
    @Operation(summary = "获得假期日历")
    @Parameter(name = "holiday_date", description = "假期日期")
    public CommonResult<List<IsHolidayDO>> getIsHolidayList(@Valid IsHolidayPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            pageReqVO.setHolidayDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
        } catch (ParseException e) {
            pageReqVO.setHolidayDate(new Date());
        }
        List<IsHolidayDO> pageResult = areaReserveService.getIsHolidayList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, IsHolidayDO.class));

    }

    @GetMapping("/getIsAdjustedWorkdayList")
    @Operation(summary = "获得调班日历")
    public CommonResult<List<IsAdjustedWorkdayDO>> getIsAdjustedWorkdayList(@Valid IsAdjustedWorkdayPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            pageReqVO.setAdjustedWorkday(dateFormat.parse(pageReqVO.getQueryDate().trim()));
        } catch (ParseException e) {
            pageReqVO.setAdjustedWorkday(new Date());
        }
        List<IsAdjustedWorkdayDO> pageResult = areaReserveService.getIsAdjustedWorkdayList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, IsAdjustedWorkdayDO.class));

    }


    @GetMapping("/RefreshAreaBookingQuota")
    @Operation(summary = "重置一个时间段内的坐席资源")
    @Parameter(name = "startDate", description = "起始日期", required = true)
    @Parameter(name = "endDate", description = "截止日期", required = true)
    public CommonResult<List<AreaBookingQuotaDO>> RefreshAreaBookingQuota(@Valid AreaBookingQuotaPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getStartDate().trim()));
            pageReqVO.setStartResourceDate(dateFormat.parse(pageReqVO.getStartDate().trim()));
            pageReqVO.setEndResourceDate(dateFormat.parse(pageReqVO.getEndDate().trim()));
        } catch (ParseException e) {
            pageReqVO.setStartResourceDate(new Date());
            pageReqVO.setEndResourceDate(new Date());
        }
        //刷新一个时间段的可预订坐席资源
        areaReserveService.RefreshAreaBookingQuota(pageReqVO);
        //读取最新的资源
        List<AreaBookingQuotaDO> pageResult = areaReserveService.getAreaBookingQuotaList(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, AreaBookingQuotaDO.class));

    }

    @GetMapping("/getUserInfo")
    @Operation(summary = "获得用户信息")
    public CommonResult<MdmEmployeeDO> getUserInfo(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

        MdmEmployeeDO mdmEmployeeDO = areaReserveService.getCurUser();
        return success(BeanUtils.toBean(mdmEmployeeDO, MdmEmployeeDO.class));
    }


    @GetMapping("/getBookingResources")
    @Operation(summary = "获得可预订坐席信息")
    @Parameter(name = "queryDate", description = "查询日期")
    @Parameter(name = "recentDays", description = "最近几天资源", example = "4")
    public CommonResult<List<BookedSeatRecordRespVO>> getBookingResources(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            if (pageReqVO.getQueryDate() != null){
                pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
            }
            else{
                Date date = new Date(); // 当前时间

                // 使用Calendar类来设置时间部分为00:00:00
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                Date dateAtStartOfDay = calendar.getTime();
                pageReqVO.setResourceDate(dateAtStartOfDay);
            }

        } catch (ParseException e) {
            Date date = new Date(); // 当前时间

            // 使用Calendar类来设置时间部分为00:00:00
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            Date dateAtStartOfDay = calendar.getTime();
            pageReqVO.setResourceDate(dateAtStartOfDay);
        }
        List<BookedSeatRecordRespVO> bookedSeatRecordList = areaReserveService.getSeatListByUser(pageReqVO);
        return success(BeanUtils.toBean(bookedSeatRecordList, BookedSeatRecordRespVO.class));
    }

    @GetMapping("/getBookingResourcesForAdmin")
    @Operation(summary = "超管获得可预订坐席信息")
    @Parameter(name = "queryDate", description = "查询日期")
    @Parameter(name = "recentDays", description = "最近几天资源", example = "4")
    public CommonResult<List<BookedSeatRecordRespVO>> getBookingResourcesForAdmin(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            if (pageReqVO.getQueryDate() != null){
                pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
            }
            else{
                Date date = new Date(); // 当前时间

                // 使用Calendar类来设置时间部分为00:00:00
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                Date dateAtStartOfDay = calendar.getTime();
                pageReqVO.setResourceDate(dateAtStartOfDay);
            }

        } catch (ParseException e) {
            Date date = new Date(); // 当前时间

            // 使用Calendar类来设置时间部分为00:00:00
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            Date dateAtStartOfDay = calendar.getTime();
            pageReqVO.setResourceDate(dateAtStartOfDay);
        }
        List<BookedSeatRecordRespVO> bookedSeatRecordList = areaReserveService.getSeatListByUserForAdmin(pageReqVO);
        return success(BeanUtils.toBean(bookedSeatRecordList, BookedSeatRecordRespVO.class));
    }


    @GetMapping("/BookSeat")
    @Operation(summary = "预订坐席")
    @Parameter(name = "canBookMultiSeat", description = "是否允许预订当日多个坐席", example = "False")
    @Parameter(name = "officeName", description = "办公室名称如", required = true, example = "上海港汇")
    @Parameter(name = "areaName", description = "区域名称", required = true, example = "A")
    @Parameter(name = "queryDate", description = "预约日期", required = true)
    @Parameter(name = "type", description = "预约类型", required = true, example = "am、pm、all")
    @Parameter(name = "status", description = "预约或取消预约", required = true, example = "True")
    @Parameter(name = "comment", description = "代订备注", required = true)
    public CommonResult<BookingUserRecordRespVO> BookSeat(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            if(pageReqVO!= null && pageReqVO.getQueryDate() != null){
                pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
            }
            else {
                pageReqVO.setResourceDate(new Date());
            }
        } catch (ParseException e) {
            pageReqVO.setResourceDate(new Date());
        }
        //读取最新的预订记录
        BookingUserRecordRespVO bookedSeatRecordRespVO = areaReserveService.BookSeat(pageReqVO);
        return success(BeanUtils.toBean(bookedSeatRecordRespVO, BookingUserRecordRespVO.class));
    }

    @GetMapping("/getBookedUserRecord")
    @Operation(summary = "获取当前用户预约记录")
    @Parameter(name = "queryDate", description = "查询日期", example = "2025-04-17")
    public CommonResult<List<BookingUserRecordRespVO>> getBookedUserRecord(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            if(pageReqVO!= null && pageReqVO.getQueryDate() != null){
                pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
            }
            else {
                pageReqVO.setResourceDate(new Date());
            }

        } catch (ParseException e) {
            pageReqVO.setResourceDate(new Date());
        }
        List<BookingUserRecordRespVO> bookedUserRecordList = areaReserveService.getBookedUserRecord(pageReqVO);
        return success(BeanUtils.toBean(bookedUserRecordList, BookingUserRecordRespVO.class));
    }

    @GetMapping("/CancelSeat")
    @Operation(summary = "取消预订坐席")
    @Parameter(name = "recordId", description = "预约记录主键", required = true)
    @Parameter(name = "officeName", description = "办公室名称如", example = "上海港汇")
    @Parameter(name = "areaName", description = "区域名称", example = "A")
    @Parameter(name = "queryDate", description = "查询日期")
    @Parameter(name = "type", description = "预约类型", example = "am")
    @Parameter(name = "status", description = "预约或取消预约", example = "False")
    public CommonResult<BookingUserRecordRespVO> CancelSeat(@Valid BookingUserRecordReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        try {
            if (pageReqVO != null && pageReqVO.getQueryDate() != null){
                pageReqVO.setResourceDate(dateFormat.parse(pageReqVO.getQueryDate().trim()));
            }
            else{
                pageReqVO.setResourceDate(new Date());
            }

        } catch (ParseException e) {
            pageReqVO.setResourceDate(new Date());
        }


        //读取最新的预订记录
        BookingUserRecordRespVO bookedSeatRecordRespVO = areaReserveService.CancelSeat(pageReqVO);
        return success(BeanUtils.toBean(bookedSeatRecordRespVO, BookingUserRecordRespVO.class));
    }



 }
